package com.sfh.dog.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.sfh.dog.DogApplication;
import com.sfh.dog.R;
import com.sfh.dog.entity.Daily;
import com.sfh.dog.entity.Dog;

/**
 * 日志列表
 * 
 * @author SunFeihu 孙飞虎
 * @version [版本号2014-3-28]
 */
public class DailyAdapter extends LBaseAdapter<Daily, DailyAdapter.HondlerView>
{
    
    public DailyAdapter(Context context, List<Daily> list)
    {
        super(context, R.layout.adapter_daily_item, list);
    }
    
    static class HondlerView
    {
        public ImageView ivPicture;
        
        public ImageView ivPhoto;
        
        // 名字
        public TextView tvName;
        
        // 简介
        public TextView tvDescribe;
        
        // 赞
        public TextView tvZan;
        
        // 留言
        public TextView tvLookRevice;
        
    }
    
    @Override
    public HondlerView findHondView(View view)
    {
        HondlerView mHondlerView = new HondlerView();
        mHondlerView.ivPicture = (ImageView)view.findViewById(R.id.ivPicture);
        mHondlerView.ivPhoto = (ImageView)view.findViewById(R.id.ivPhoto);
        mHondlerView.tvName = (TextView)view.findViewById(R.id.tvName);
        mHondlerView.tvDescribe = (TextView)view.findViewById(R.id.tvDescribe);
        mHondlerView.tvZan = (TextView)view.findViewById(R.id.tvZan);
        mHondlerView.tvDescribe = (TextView)view.findViewById(R.id.tvDescribe);
        mHondlerView.tvZan = (TextView)view.findViewById(R.id.tvZan);
        mHondlerView.tvLookRevice = (TextView)view.findViewById(R.id.tvLookRevice);
        return mHondlerView;
    }
    
    @Override
    public void setViewData(Daily t, final HondlerView hondler, int position)
    {
        hondler.tvZan.setText(t.getPraiseSize() + "");
        hondler.tvLookRevice.setText(t.getReviewSize() + "");
        hondler.tvDescribe.setText(t.getDescription());
        hondler.ivPhoto.setTag(t.getDogID());
        hondler.ivPhoto.setOnClickListener(clickListener);
        
        ImageLoader.getInstance().loadImage(t.getPicture(),
            DogApplication.options,
            new SimpleImageLoadingListener()
            {
                
                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage)
                {
                    hondler.ivPicture.setImageBitmap(loadedImage);
                }
                
            });
        BmobQuery<Dog> query = new BmobQuery<Dog>();
        query.addWhereEqualTo("objectId", t.getDogID());
        query.findObjects(mContext, new FindListener<Dog>()
        {
            
            @Override
            public void onSuccess(List<Dog> arg0)
            {
                if (arg0 == null || arg0.size() ==0)
                {
                    return;
                }
               
                hondler.tvName.setText(arg0.get(0).getName());
                hondler.tvName.setCompoundDrawablesWithIntrinsicBounds(mContext.getResources().getDrawable(arg0.get(0)
                    .getSex() == 0 ? R.drawable.female : R.drawable.male),
                    null,
                    null,
                    null);
                ImageLoader.getInstance().displayImage(arg0.get(0).getImage(),
                    hondler.ivPhoto,
                    DogApplication.options);
            }
            
            @Override
            public void onError(int arg0, String arg1)
            {
                
            }
        });
        
    }
    
    private OnClickListener clickListener = new OnClickListener()
    {
        
        @Override
        public void onClick(View v)
        {
            String dogID = String.valueOf(v.getTag());
            DogApplication.gotoLookADogUI(mContext, dogID);
        }
    };
}
